/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.just.dusin.ooii.component.kj;

import java.sql.SQLException;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.logging.Level;
import org.apache.log4j.Logger;
import org.dusin.webcoal.exception.OOIIException;
import org.just.dusin.ooii.component.Sink;
import org.just.dusin.ooii.database.CLYCBean;
import org.just.dusin.ooii.database.DataOf5mBean;
import org.just.dusin.ooii.database.DatabaseConnectionSQL;
import org.just.dusin.ooii.database.MineData;
import org.just.dusin.ooii.database.OrgDao4SQLServer;
import org.just.dusin.ooii.database.QHSSSJBean;
import org.just.dusin.ooii.database.QHSSSJDao;
import org.just.dusin.ooii.util.DateUtil;

/**
 *
 * @author Administrator
 */
public class SpecialQHUploadSink implements Sink{
    
    private static Logger logger = Logger.getLogger(SpecialQHUploadSink.class.getName());
    
    private DatabaseConnectionSQL dbc=null;
    private QHSSSJDao qhsssjDao =null;
    OrgDao4SQLServer orgDao = null;

    public DatabaseConnectionSQL getDbc() {
        return dbc;
    }

    public void setDbc(DatabaseConnectionSQL dbc) {
        this.dbc = dbc;
    }
    
    int smycOld=-1;
    int jlycOld=-1;
    int wkycOld=-1;
    int txycOld=-1;
    

    @Override
    public void process(String msg) throws OOIIException {
        try {
            qhsssjDao = new QHSSSJDao(dbc.getConnection());
            orgDao = new OrgDao4SQLServer(dbc.getConnection());
        } catch (SQLException ex) {
            dbc.close();
            logger.error(ex.getMessage());
            throw new OOIIException(ex);
        }
        try {
            DataPackage.setOrgDao(orgDao);
            logger.debug("准备处理" + msg);
            List<MineData> mineDatas = DataPackage.dePack(msg);  //重复数据要排除
            for (MineData mineData : mineDatas) {
                if (mineData instanceof DataOf5mBean) {
                    DataOf5mBean d5m = (DataOf5mBean) mineData;                    
                    
                    QHSSSJBean qhsssjBean = new  QHSSSJBean();
                    qhsssjBean.setMKBH(d5m.getMineCode());
                    qhsssjBean.setSHI(d5m.getCityName());
                    qhsssjBean.setXIAN(d5m.getCountyName());
                    qhsssjBean.setMKMC(d5m.getMineName());
                    qhsssjBean.setSBBH("1");
                    qhsssjBean.setTSFS(1);
                    qhsssjBean.setYXH(0);
                    String dataTimeString = DateUtil.getString(d5m.getYear(), d5m.getMonth(), d5m.getDay(), d5m.getHour(), d5m.getMin(), 0);
                    qhsssjBean.setCJSJ(dataTimeString);
                    qhsssjBean.setCC(1);
                    qhsssjBean.setXSZL(d5m.getSumof5min());
                    qhsssjBean.setSMYC(0);
                    qhsssjBean.setJLYC(0);
                    qhsssjBean.setWKYC(0);
                    qhsssjBean.setTXYC(0);
                    qhsssjBean.setFSZT(0);
                    qhsssjBean.setYCBS("0");
                    qhsssjDao.doCreate(qhsssjBean);
                    logger.info("处理完5分钟产量数据："+msg);
                
            } else if (mineData instanceof CLYCBean) {                

                    CLYCBean clyc = (CLYCBean) mineData;                    
                    QHSSSJBean qhsssjBean = new  QHSSSJBean();
                    qhsssjBean.setMKBH(clyc.getMineCode());
                    qhsssjBean.setSHI(clyc.getCityName());
                    qhsssjBean.setXIAN(clyc.getCountyName());
                    qhsssjBean.setMKMC(clyc.getMineName());
                    qhsssjBean.setSBBH("1");
                    qhsssjBean.setTSFS(1);
                    qhsssjBean.setYXH(0);
                    String dataTimeString = DateUtil.getString(clyc.getYear(), clyc.getMonth(), clyc.getDay(), clyc.getHour(), clyc.getMin(), 0);
                    qhsssjBean.setCJSJ(dataTimeString);
                    qhsssjBean.setCC(1);
                    qhsssjBean.setXSZL(0);

                    String status=clyc.getStatus();
                    int smyc = status.charAt(0) - '0';
                    int jlyc = status.charAt(1) - '0';
                    int wkyc = status.charAt(2) - '0';
                    int txyc = status.charAt(3) - '0';        

                    if(smycOld!=smyc||jlycOld!=jlyc||wkycOld!=wkyc||txycOld!=txyc){
                        qhsssjBean.setSMYC(smyc);
                        qhsssjBean.setJLYC(jlyc);
                        qhsssjBean.setWKYC(wkyc);
                        qhsssjBean.setTXYC(txyc);
                        qhsssjBean.setFSZT(0);
                        qhsssjBean.setYCBS("1");
                        qhsssjDao.doCreate(qhsssjBean);
                        smycOld=smyc;
                        jlycOld=jlyc;
                        wkycOld=wkyc;
                        txycOld=txyc;
                    }
                    logger.info("处理完异常数据："+msg);
                }
            }
        } catch (SQLException ex) {
            dbc.close();
            logger.error(ex.getMessage());
            throw new OOIIException(ex);
        }catch(Exception e){
            dbc.close();
            logger.error(e.getMessage());
            throw new OOIIException(e);
        }
    }

    @Override
    public void process(List<String> msgs) throws OOIIException {
        for (String msg : msgs) {
            process(msg);
        }
    }

    @Override
    public void initComponent(Properties props) {        
    
    }

    @Override
    public void destroyComponent() throws Exception {
        
    }
    
}
